package common.fengkai.filepost.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import common.fengkai.filepost.VO.ResVoTO;
import common.fengkai.filepost.entity.Position;
import common.fengkai.filepost.entity.Seal;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.FileWriter;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.SecureCacheResponse;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;

@Slf4j
public  class TestEsealForHttpClient电子保单 implements Runnable{
    private   int i =0;

       public static void main(String[] args) throws Exception {
           for (int i = 0; i <1 ; i++) {
               try {
                   System.out.println("第"+(i+1)+"次签章");
                   TestEsealForHttpClient电子保单.getSignpDF();
               } catch (Exception e) {
                   e.printStackTrace();
               }
           }
       }




    public static void getSignpDF () throws Exception {
        long currentTimeMillis = System.currentTimeMillis();

        //soap服务地址
        // String url = "http://10.0.100.57:7001/iSignatureWebPDF/services/PdfActionService";
        //String url = "http://10.29.28.3/eseal/eseal/app/services/signPdf";
        //String url = "http://10.30.48.249:81/eseal2/app/services/signPdf";
        //String url = "http://10.29.28.3:81/eseal2/app/services/signPdf";
        //String url = "http://10.29.31.122:8882/eseal2/eseal2/app/services/signPdf";
        String url = "http://10.29.31.124:8002/eseal/app/services/signPdf";
        //String url = "http://10.29.31.58:8002/eseal/app/services/signPdf";

         //String url = "http://10.30.48.249/eseal/eseal/app/services/signPdf";
        //String url = "http://10.30.48.249:81/eseal2/app/services/signPdf";
        TestEsealForHttpClient电子保单 testEsealForHttpClient = new TestEsealForHttpClient电子保单();
        Map<String, Object> map = new LinkedHashMap<String,Object>();
        map.put("json", TestEsealForHttpClient电子保单.getJsonString());

        HttpClient httpClient = HttpClients.createDefault();
        HttpPost httpRequest = new HttpPost(url);
        //设置超时时间
        RequestConfig builder = RequestConfig.custom().setConnectTimeout(120000).setSocketTimeout(120000).build();
        httpRequest.setConfig(builder);
        httpRequest.setHeader("Content-Type", "application/json");
        httpRequest.setEntity(new StringEntity(TestEsealForHttpClient电子保单.getJsonString(),"UTF-8"));
        String jsonString = TestEsealForHttpClient电子保单.getJsonString();
        FileWriter fileWriter = null;
          try {
              fileWriter=new FileWriter("C:\\Users\\carry\\Desktop\\新建文本文档(3).txt");
              fileWriter.write(jsonString);
          } catch(Exception e) {
              e.printStackTrace();
          }finally {
              fileWriter.flush();
              fileWriter.close();
          }


        HttpResponse response = httpClient.execute(httpRequest);

        HttpEntity entity = response.getEntity();
        if (response.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) {
            String result = EntityUtils.toString(entity);
            JSONObject jsonObject = JSONObject.parseObject(result);
            ResVoTO responVo = JSON.toJavaObject(jsonObject, ResVoTO.class);
            String file = responVo.getFile();
            Base64util.decoderBase64File(file,"C:\\Users\\carry\\Desktop\\电子保单测试\\更新后signed915.pdf","C:\\Users\\carry\\Desktop\\电子保单测试");
//            System.out.println(result);
            long currentTimeMillis1 = System.currentTimeMillis();
            System.out.println("花费了"+(currentTimeMillis1-currentTimeMillis)/1000+"秒");
        }


    }

   private static String getJsonString () throws Exception {
       String base64File = Base64util.encodeBase64File("C:\\Users\\carry\\Desktop\\电子保单测试\\更新后.pdf");
       //
       Signature signature = new Signature();
       signature.setSignPdf(base64File);
       List<SignSeal> signSealList = new ArrayList<>();
       //公章  圆形  太平财产保险有限公司-新洲支公司   太平财产保险有限公司陕西分公司
       //业务章 签名章 方形 太平财产保险有限公司青岛分公司承保电子专用章  太平财产保险有限公司深圳分公司中英文许少武授权签名章
       Seal seal1 = new Seal("太平财产保险有限公司深圳分公司理赔专用章","sichuan");
       Seal seal2 = new Seal("太平财产保险有限公司深圳分公司理赔专用章","sichuan");
       Position position1 = new Position(1,450,300,null);
       Position position2 = new Position(0,null,null,"保险人签章");
       SignSeal signSeal1 = new SignSeal("swxa",seal1,position1,1);
       SignSeal signSeal2 = new SignSeal("swxa",seal2,position2,1);
       //signSealList.add(signSeal1);
       signSealList.add(signSeal2);
       signature.setSignature(signSealList);
       String jsonString = JSONObject.toJSONString(signature);
       System.out.println("jsonString="+jsonString);
       return jsonString;

   }


    @Override
    public  void run () {
        i++;
        try {
            TestEsealForHttpClient电子保单.getSignpDF();
            System.out.println(Thread.currentThread().getId()+"号线程进行签章");
            System.out.println("总共进行了"+i+"次签章");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
